package newsletterService

import (
	"fmt"
	"github.com/Sir-yuChen/funAnalyse/global"
	"github.com/Sir-yuChen/funAnalyse/global/constant"
	"github.com/Sir-yuChen/funAnalyse/model"
	"github.com/Sir-yuChen/funAnalyse/model/common/ApiReturn"
	"github.com/Sir-yuChen/funAnalyse/model/request"
	"github.com/Sir-yuChen/funAnalyse/model/resp"
	"github.com/Sir-yuChen/funAnalyse/service/basisService/file"
	"github.com/Sir-yuChen/funAnalyse/service/commonService"
	"github.com/Sir-yuChen/funAnalyse/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)

type StockUserGroupService struct{}

func NewStockUserGroupService() *StockUserGroupService {
	return &StockUserGroupService{}
}

//GetStockUserGroup 多条件查询
func (u *StockUserGroupService) GetStockUserGroup(req *model.StockUserGroupModel) (a ApiReturn.ApiReturnCode) {
	var mo []model.StockUserGroupModel
	sql := ""
	obj := model.NewStockUserGroupModel()
	tableName := obj.TableName()
	sqlCondition := utils.ReflectHandleStuct(req)
	if sqlCondition != "" {
		sql = `select * from ` + tableName + ` where ` + sqlCondition
	} else {
		sql = `select * from ` + tableName + ` order by id desc  limit 100 `
	}
	err := global.GVA_DB.Raw(sql).Scan(&mo).Error
	if err != nil {
		global.GVA_LOG.Error("GetStockBulletinGroup异常", zap.Any("request", req), zap.Any("sql", sql), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	s := ApiReturn.OK
	s.Data = mo
	return s
}

//InsertStockUserGroup 新增用户群
func (u *StockUserGroupService) InsertStockUserGroup(req *model.StockUserGroupModel, c *gin.Context) (a ApiReturn.ApiReturnCode) {
	if req.GroupType == int64(constant.USER_GROUP_TYPE_FILE) {
		//上传文件返回Id
		var uploadFileReq request.UploadFileRequest
		uploadFileReq.FileEffect = "发送公告信息"
		uploadFileReq.FileType = 1
		apiReturn := file.NewStockFilesService().UploadFiles(&uploadFileReq, c)
		if apiReturn.Code != ApiReturn.OK.Code {
			return apiReturn
		}
		data := apiReturn.Data
		responses, ok := data.([]resp.UplocadeFilesResponse)
		if ok {
			for _, respons := range responses {
				if req.FileId == "" {
					req.FileId = fmt.Sprint(respons.FileId)
				} else {
					req.FileId += "," + fmt.Sprint(respons.FileId)
				}
			}
		} else {
			global.GVA_LOG.Error("新增用户群,上传文件解析返回参数异常", zap.Any("respons", apiReturn))
			return ApiReturn.ErrSystem
		}
	}
	userInfo, err := commonService.GetUserInfoByToken(c)
	if err != nil {
		global.GVA_LOG.Error("获取操作账户信息异常", zap.Error(err))
		return ApiReturn.ErrSystem
	}
	req.CreateBy = userInfo.CustomerName
	if req.Status == "" {
		req.Status = constant.STATUS_Y
	}
	now := model.LocalTime(time.Now())
	req.CreateTime = &now
	req.LastUpdTime = &now
	if err = global.GVA_DB.Model(model.StockUserGroupModel{}).Create(req).Error; err != nil {
		global.GVA_LOG.Error("InsertStockBulletinGroup异常", zap.Any("request", req), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	return ApiReturn.OK
}

//UpdateStockUserGroup 更新
func (u *StockUserGroupService) UpdateStockUserGroup(req *model.StockUserGroupModel) (a ApiReturn.ApiReturnCode) {
	if req.Id == 0 {
		return ApiReturn.ErrParam
	}
	err := global.GVA_DB.Model(model.StockUserGroupModel{}).Updates(req).Error
	if err != nil {
		global.GVA_LOG.Error("UpdateStockBulletinGroup异常", zap.Any("request", req), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	return ApiReturn.OK
}

//DelStockUserGroup 根据ID删除
func (u *StockUserGroupService) DelStockUserGroup(req *model.StockUserGroupModel) (a ApiReturn.ApiReturnCode) {
	if req.Id == 0 {
		return ApiReturn.ErrParam
	}
	err := global.GVA_DB.Model(model.StockUserGroupModel{}).Where("id = ?", req.Id).Delete(model.StockUserGroupModel{}).Error
	if err != nil {
		global.GVA_LOG.Error("DelStockBulletinGroup异常", zap.Any("request", req), zap.Error(err))
		return ApiReturn.ErrSystem
	}
	return ApiReturn.OK
}
